//验证码
function checkCode(){
    Ajax({
        url:"http://localhost/project/server/checkCode.php",
        success(data){
            sessionStorage.setItem("checkCode",data)
            const checkCode = document.getElementById("checkCode")
            checkCode.innerHTML = data
        }
    })
}
checkCode()

//登录校验
function checkLogin(){
    //账号密码长度校验
    const username = document.getElementById("username")
    const password = document.getElementById("password")
    function fn1(){
        if(/^\w{5,16}$/i.test(username.value)){
            i1.style.opacity = "1"
            a1.style.opacity = "0"
        }else{
            a1.style.opacity = "1"
            i1.style.opacity = "0"
        }
    }
    function fn2(){
        if(/^\w{5,16}$/i.test(password.value)){
            i2.style.opacity = "1"
            a2.style.opacity = "0"
        }else{
            a2.style.opacity = "1"
            i2.style.opacity = "0"
        }
    }
    username.oninput = debounce(fn1,1000)
    password.oninput = debounce(fn2,1000)
    function debounce(fn,time){
        let timer = null
        return function(){
            if(timer){clearTimeout(timer)}
            timer = setTimeout(() => {
                fn()
            }, time);
        }
    }
    //点击登录发送ajax校验账号密码
    const form = document.querySelector("form")
    form.onsubmit = function(e){
        e = e || window.event
        e.preventDefault()
    const username1 = document.getElementById("username")
    const password1 = document.getElementById("password")
    if(username1.value.trim().length == 0 || password1.value.trim().length == 0){
        return alert("账号或密码不能为空")
    }
        //验证码校验
        let checkCode = sessionStorage.getItem("checkCode")
        if(check.value != checkCode){
            return alert("验证码错误")
        }
        Ajax({
            url:"http://localhost/project/server/userInfo.php",
            success(msg){
                let data = JSON.parse(msg)
                if(username1.value == data[0].username && password1.value == data[0].password){
                    //存储一个登录标识符
                    sessionStorage.setItem("login",true)
                    sessionStorage.setItem("manageInfo",msg)
                    location.href = "http://localhost/project/html/home.html"
                    
                }else{
                    return alert("账号或密码错误")
                }
            }
        })
        }
    }
    checkLogin()

//点击记住密码
function remember(){
    let num = 1
    box1.onclick = function(){
    const username1 = document.getElementById("username")
    const password1 = document.getElementById("password")
        num++
        if(num % 2 == 0){
            i3.style.opacity = "1"
            username1.value = "admin"
            password1.value = "123456"
        }else if(num % 2 != 0){
            i3.style.opacity = "0"
            username1.value = ""
            password1.value = ""
        }
    }
}
remember()
//点击眼睛密码框可视
function eyeClick(){
    const eye = document.getElementById("eye")
    eye.onclick = function(){
        const password = document.getElementById("password")
        if(password.type == "password"){
            password.type = "text"
        }else{
            password.type = "password"
        }
    }
}
eyeClick()

//点击验证码刷新验证码
function checkCodeClick(){
    const checkCode = document.getElementById("checkCode")
    checkCode.onclick = function(){
        Ajax({
            url:"http://localhost/project/server/checkCode.php",
            success(data){
                sessionStorage.setItem("checkCode",data)
                const checkCode = document.getElementById("checkCode")
                checkCode.innerHTML = data
            }
        })
    }
}
checkCodeClick()
